In computer science, a virtual machine is software that acts as an interface between compiler binary code and the microprocessor (or other hardware platform) that actually performs the machine's instructions. This allows software written for a particular platform to be run on any platform compatible with the virtual machine, without the need for code to be rewritten or recompiled by the programmer for each separate platform. Java Virtual Machine™ (Java VM) from Sun Microsystems of Palo Alto, Calif. is an example of a virtual Machine. Once a Java VM has been provided for a platform, any Java program can run on that platform.
If an application executed by a virtual machine is written in an object-oriented language, the executable code of the application typically takes form of a number of classes that the virtual machine loads in memory and executes. Again, the Java language and its implementation are an example of this.
A class in an object-oriented language can be defined in multiple ways, but for the purposes of this application a class is defined as a collection of methods. A method, in turn, is a chunk of executable code that cab be called (or executed) by other methods from its own class or other classes.
Applications executed by a virtual machine often make the virtual machine load many classes, each containing numerous methods. This is typically due to the fact that library classes are used extensively. A library is a (typically large) collection of classes that implement some common functionality. The so-called core classes defined in the Java language implementation is an example of a class library. While libraries provide convenience for the programmer, allowing them to utilize readily available classes instead of writing the necessary code themselves, it may also waste memory space and processor time. This is due to the fact that only a fraction of methods contained in a typical library class are actually used, i.e., called directly or indirectly by the application code. Nevertheless, the virtual machine dynamically loads into memory a complete representation of each class, even if only one method of this class is actually used. As a result, a significant amount of main memory is wasted at run time. Additionally, virtual machines typically manage memory automatically by performing garbage collection, during which objects in memory are moved around. Thus, the more wasted space utilized in memory, the more time needed to manage it.
FIG. 1 is a diagram illustrating an example of how a virtual machine is typically loaded with classes. Class C 100 has four methods, M1 102, M2 104, M3 106, and M4 108. Class C 100 is contained on disk 110. At execution, the virtual machine loads all of the methods M1 112, M2 114, M3 116, and M4 118 into its memory. However, perhaps only M2 and M4 are referenced in the application code (methods which are referenced in the application code are indicated as such by shading). Therefore, this solution causes the virtual machine to unnecessarily load M1 and M3, which slows execution and wastes memory space.
What is needed is a solution that reduces the size and speed drawbacks of loading complete classes into virtual machines.